package ug.co.icom.kyazze.healthticket;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import ug.co.icom.kyazze.healthticket.data.User;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {
	// Splash screen timer
    private static int SPLASH_TIME_OUT = 5000;
    Locale locale = Locale.getDefault();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	getActionBar().hide();
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen_layout);
 
        
        new Handler().postDelayed(new Runnable() {
 
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */
 
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
            	SharedPreferences settings = getSharedPreferences("ULS", 0);
            	
            	if(settings.getBoolean("rem", false))
            	{
            		User user = new User();
            		user.PhoneNumber = settings.getString("uName", "");
            		user.PassCode = settings.getString("pWord", "");
            		user.FirstName = settings.getString("fName", "");
            		user.LastName = settings.getString("lName", "");
            		user.OtherNames = settings.getString("oName", "");
            		user.Address = settings.getString("add", "");
            		user.BloodType = settings.getString("bt", "");
            		user.Female = settings.getBoolean("fem", false);
            		user.DateOfBirth = getDateFromString(settings.getString("dob", ""));
            		user.Email = settings.getString("ema", "");
            		if(!user.PhoneNumber.equals(""))
            		{
            			Intent i = new Intent(SplashScreen.this, MainActivity.class);
            			i.putExtra("user", user);
                        startActivity(i);
            		}
            		else
            		{
            			Intent i = new Intent(SplashScreen.this, LoginActivity.class);
                        startActivity(i);
            		}
            	}
            	else
            	{
            		Intent i = new Intent(SplashScreen.this, LoginActivity.class);
                    startActivity(i);
            	}               
 
                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
    
    private Date getDateFromString(String dateReceived) {
		SimpleDateFormat  format = new SimpleDateFormat("dd-MM-yyyy", locale);  
		try {  
		    Date date = format.parse(dateReceived);  
		    return date;  
		} catch (Exception e) {  
		    return new Date();
		}
	}

}
